package com.mmdccj.yugu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mmdccj.yugu.dto.MusicDto;
import com.mmdccj.yugu.dto.MusicListDto;
import com.mmdccj.yugu.dto.PlayDto;
import com.mmdccj.yugu.entity.Music;
import com.mmdccj.yugu.entity.Play;
import com.mmdccj.yugu.entity.User;
import com.mmdccj.yugu.mapper.MusicListMapper;
import com.mmdccj.yugu.mapper.MusicMapper;
import com.mmdccj.yugu.mapper.UserMapper;
import com.mmdccj.yugu.service.MusicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: MMDCCJ
 * @date: 2023-11-27 23:56
 */
@Slf4j
@Service
public class MusicServiceImpl implements MusicService {
    // 自动注入MusicListMapper实例
    @Autowired
    private MusicListMapper musicListMapper;
    // 自动注入MusicMapper实例
    @Autowired
    private MusicMapper musicMapper;
    // 自动注入UserMapper实例
    @Autowired
    private UserMapper userMapper;

    /**
     * 获取歌单页面需要的信息
     *
     * @param musicListId 歌单id
     * @return MusicListDto
     */
    @Override
    public MusicListDto getMusicByMusicList(String musicListId) {
        // 1.先查询歌单页面需要的数据
        MusicListDto dto = musicMapper.getListInfoByMusicList(musicListId);
        if (dto == null) {
            return null;
        }
        // 2.再查询属于该歌单的音乐并进行拼接
        dto.setMusicList(musicMapper.getMusicByMusicList(musicListId));
        return dto;
    }

    @Override
    public Music getMusicById(String musicId) {
        return musicMapper.getMusicById(musicId);
    }

    @Override
    public int getMusicNumFromMusicListByMusicListId(String id) {
        return musicListMapper.getMusicNumFromMusicListByMusicListId(id);
    }

    @Override
    public MusicDto getMusicAndSingerById(String musicId) {
        Music music = musicMapper.getMusicById(musicId);
        String[] userId = userMapper.getUserIdByMusicId(musicId);
        List<User> users = userMapper.getUsersByIds(userId);
        MusicDto musicDto = new MusicDto();
        musicDto.setMusic(music);
        musicDto.setUserList(users);
        return musicDto;
    }

    @Override
    public List<Music> getSingerAllMusicById(String userId) {
        return musicMapper.getSingerAllMusicById(userId);
    }

    @Override
    public void updatePlayTimes(String userId, String musicId, String typeId, String type) {
        musicMapper.updateMusicPlays(musicId);
        if (type == "musicList") {
            musicListMapper.updateListPlays(typeId);
        } else if (type == "cd") {
            // Todo cd暂时未实现
        }
        if (userId != null) {
            // 首次播放
            if (userMapper.isFirstPlay(userId, musicId) == 0) {
                userMapper.insertPlay(userId, musicId, LocalDateTime.now());
            } else {
                // 非首次播放
                userMapper.updateUserPlays(userId, musicId, LocalDateTime.now());
            }
        }
    }

    @Transactional
    @Override
    public List<PlayDto> getUserWeekPlay(String userId) {
        List<Play> playList = userMapper.getUserWeekPlay(userId);
        List<String> musicIds = new ArrayList<>();
        for (Play play : playList) {
            musicIds.add(play.getMusicId());
        }
        List<String> userIds = new ArrayList<>();
        if (musicIds.size() == 0) return null;
        for (String musicId : musicIds) {
            userIds.add(userMapper.getUserIdByMusicId(musicId)[0]);
        }
        List<PlayDto> playDto = new ArrayList<>();
        for (int i = 0; i < playList.size(); i++) {
            PlayDto dto = new PlayDto();
            dto.setMusic(musicMapper.getMusicById(musicIds.get(i)));
            dto.setUser(userMapper.getPublicUserInfo(userIds.get(i)));
            dto.setCount(playList.get(i).getPlayCount());
            playDto.add(dto);
        }
        return playDto;
    }

    @Transactional
    @Override
    public List<PlayDto> getAllPlay(String userId) {
        List<Play> playList = userMapper.getUserAllPlay(userId);
        List<String> musicIds = new ArrayList<>();
        for (Play play : playList) {
            musicIds.add(play.getMusicId());
        }
        if (musicIds.size() == 0) return null;
        List<String> userIds = userMapper.getUserIdsByMusicIds(musicIds);
        List<PlayDto> playDto = new ArrayList<>();
        for (int i = 0; i < playList.size(); i++) {
            PlayDto dto = new PlayDto();
            dto.setMusic(musicMapper.getMusicById(musicIds.get(i)));
            dto.setUser(userMapper.getPublicUserInfo(userIds.get(i)));
            dto.setCount(playList.get(i).getPlayCount());
            playDto.add(dto);
        }
        return playDto;
    }

    @Override
    @Transactional
    public boolean insertMusic(String userId, Integer duration, String title, String music, String image, String intro, String style) {
        musicMapper.insertMusic(title, intro, duration, image, music, LocalDateTime.now(), music.split("\\.")[1], style);
        musicMapper.insertMusicLinkSinger(userId, musicMapper.getMusicIdBySrc(music));
        return true;
    }

    @Override
    public List<Music> getUserMusic(String userId) {
        return musicMapper.getUserMusic(userId);
    }

    @Override
    public void updateMusic(String musicId, Integer duration, String title, String music, String image, String intro, String style) {
        musicMapper.updateMusic(musicId, duration, title, music, image, intro, music.split("\\.")[1], style);
    }

    @Override
    public List<Music> getUserAllMusicWithoutCD(String userId) {
        return musicMapper.getUserAllMusicWithoutCD(userId);
    }

    /**
     * 删除所有与此歌曲有关信息
     *
     * @param userId
     * @param musicId
     */
    @Override
    @Transactional
    public void deleteSingle(String userId, String musicId) {
        musicMapper.delete(musicId);
        musicMapper.deleteLinkSinger(userId, musicId);
        musicMapper.deleteLinkList(musicId);
        userMapper.deletePlays(musicId);
    }

    @Override
    public PageInfo<MusicDto> getAllMusicDto(int currentPage) {
        PageHelper.startPage(currentPage, 20);
        return new PageInfo<>(musicMapper.getAllMusic());
    }

    @Override
    public Integer getAllPlayNums(String userid) {
        List<Play> playList = musicMapper.getAllPlayNums(userid);
        int sum = 0;
        for (Play play : playList) {
            sum += play.getPlayCount();
        }
        return sum;
    }

    @Override
    public List<MusicDto> getMostLikeMusic() {
        return musicMapper.getMostLikeMusic();
    }

    @Override
    public List<MusicDto> getNewMusic() {
        return musicMapper.getNewMusic();
    }
}
